<!--创建图片页面-->
<template>
  <div id="addPicturePage">
    <h2 style="margin-bottom: 16px">
      {{ route.query?.id ? '修改图片' : '创建图片' }}
    </h2>
    <!--图片上传组件-->
    <PictureUpload :picture="picture" :onSuccess="onSuccess" />
    <!--图片信息表单 picture 为空则表示图片未上传,即上传图片后才展示更多表单项-->
    <a-form v-if="picture" layout="vertical" :model="pictureForm" @finish="handleSubmit">
      <a-form-item label="名称" name="name">
        <a-input v-model:value="pictureForm.name" placeholder="请输入名称" />
      </a-form-item>
      <a-form-item label="简介" name="introduction">
        <a-textarea
          v-model:value="pictureForm.introduction"
          placeholder="请输入简介"
          :autoSize="{minRows: 1, maxRows: 5}"
          allowClear
        />
      </a-form-item>
      <a-form-item label="分类" name="category">
        <a-auto-complete
          v-model:value="pictureForm.category"
          :options="categoryOptions"
          placeholder="请输入分类"
          allowClear
        />
      </a-form-item>
      <a-form-item label="标签" name="tags">
        <a-select
          v-model:value="pictureForm.tags"
          :options="tagsOptions"
          mode="tags"
          placeholder="请输入标签"
          allowClear
        />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit" style="width: 100%">创建</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<script setup lang="ts">
import {onMounted, reactive, ref} from "vue";
import PictureUpload from "@/components/PictureUpload.vue";
import {editPictureUsingPost, getPictureVoByIdUsingGet, listPictureTagCategoryUsingGet} from "@/api/fileController";
import {message} from "ant-design-vue";
import {useRoute, useRouter} from "vue-router";

const picture = ref<API.PictureVO>();
const pictureForm = reactive<API.PictureEditRequest>({});
/**
 * 图片上传成功回调
 * @param newPicture
 */
const onSuccess = (newPicture: API.PictureVO) =>{
  picture.value = newPicture;
  pictureForm.name = newPicture.name;
}

const router = useRouter();
/**
 * 提交表单
 */
const handleSubmit = async (values:any) => {
  const pictureId = picture.value.id;
  if (!pictureId){
    return
  }
  const res = await editPictureUsingPost({
    id: pictureId,
    ...values,
  })
  //操作成功
  if (res.data.code === 0 && res.data.data){
    message.success("创建成功");
    //跳转到图片详情页
    router.push({
      path: `/picture/${pictureId}`
    })
  }else {
    message.error("创建失败，"+res.data.message);
  }
}

const categoryOptions = ref<String[]>([]);
const tagsOptions = ref<String[]>([]);
/**
 * 获取分类和标签选项
 */
const getTagCategoryOptions = async () =>{
  const res = await listPictureTagCategoryUsingGet();
  if (res.data.code === 0 && res.data.data){
    //转换成下拉选项组件接受的格式
    tagsOptions.value
      = (res.data.data.tagList??[]).map((data:String) =>{
      return {
        value: data,
        label: data,
      }
    })
    categoryOptions.value
      = (res.data.data.categoryList??[]).map((data:String) =>{
      return {
        value: data,
        label: data,
      }
    })
  }else {
    message.error("加载选项失败失败，"+res.data.message);
  }
}

const route = useRoute()
/**
 * 获取旧数据
 */
const getOldPicture = async() =>{
  //获取数据
  const id = route.query?.id
  if (id){
    const res = await getPictureVoByIdUsingGet({
      id: id,
    })
    if (res.data.code === 0 && res.data.data){
      const data = res.data.data
      picture.value = data
      pictureForm.name = data.name
      pictureForm.introduction = data.introduction
      pictureForm.category = data.category
      pictureForm.tags = data.tags
    }
  }
}
//首次进入页面的时候获取
onMounted(()=>{
  getTagCategoryOptions();//获取分类和标签选项
  getOldPicture();//获取旧数据
})
</script>

<style scoped>
#addPicturePage {
  max-width: 720px;
  margin: 0 auto;
}
</style>
